* This code replicates the main results in Tables 7 and 8 of 
* Product Innovation and Credit Market Disruptions (forthcoming RFS)
* by Joao Granja and Sara Moreira
*
* The main data set is mainfirmgroup_placebo.dta
* This is a pseudo data set that illustrates the format of the original data set using data at the firm product line level from Nielsen RMS

clear all
set more off

use "main_firmXgroup_placebo.dta", clear
tsset firmgroup t

// Defining Age of Product Group (in Quarters)
// Step 1: Defining Cohort and Age of Product Group
rename cohort_ig cohort_i
egen cohort_ig = min(quarter),by(firmgroup) // Cohort of the Product
gen age_ig = quarter  - cohort_ig // Age of the Product

// Step 2: Defining Maximum Age of the Product Group and Exit
egen maxage_ig = max(age_ig),by(firmgroup) // 
gen exit_ig = maxage_ig == age_ig 
replace exit_ig = 0 if quarter==40 // Cohorts that we see on Qrt are right-censored. We mark them as non-exit

// Step 2.1. -> Indicator whether the product group survives more than one year
gen survival3yr = maxage_ig > 11 if cohort_ig<28
gen survival2yr = maxage_ig > 8 if cohort_ig<32
gen survival1yr = maxage_ig > 4 if cohort_ig<36

// Step 3 -> Do not consider sales of product groups after 4 years and Incumbent product groups
replace age_ig = . if age_ig>16
drop if cohort_ig==1

// Step 4 -> Defining Crisis Quarters
gen crisis = quarter>10 & quarter<21
gen crisis_cohort = cohort_ig > 10 & cohort_ig < 21

// Label Variables
gen sblshock = -bartik
egen std = std(sblshock)

label var sblshock "Local Credit Shock"
label var crisis "I(Crisis)"

// Fixed Effects and Clusters
egen st = mode(statecode), by(firm)

********************************************************************************
// Table 7: Credit-Market Disruption and Outcomes of New Products
********************************************************************************

preserve
gen n =1 

gcollapse (sum) sumrevenue (mean) rev_igt_rev_it survival1yr std (count) n if age_ig>0 & age_ig<17, by(firmgroup group cohort_i cohort_ig crisis_cohort firm st)

tab n
gen lnrev = ln(sumrevenue)
egen g = group(group cohort_ig)

label var std "Local Credit Shock"
label var crisis_cohort "I(Crisis Cohort)"

eststo: reghdfe lnrev c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lnrev c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig , absorb(g firm) vce(cl st)
eststo: reghdfe rev_igt_rev_it c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe rev_igt_rev_it c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)

esttab using "Table7.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear

restore

********************************************************************************
// Table 8 Panel A: Credit-Market Disruption and Outcomes of New Products: Potential Channels
********************************************************************************

preserve
gen n =1 

gcollapse (sum) sumrevenue (mean) parent_code fips_dma_code products_igt survival1yr std (count) n  if  age_ig>0 & age_ig<17, by(firmgroup group cohort_i cohort_ig crisis_cohort firm st)

tab n
gen lnrev = ln(sumrevenue)
gen lnrevprod = ln(sumrevenue/products_igt)
gen lnprod = ln(products_igt)
gen lnrevchain = ln(sumrevenue/parent_code)
gen lnchains = ln(parent_code)
gen lnrevdma = ln(sumrevenue/fips_dma_code)
gen lndma = ln(fips_dma_code)

label var std "Local Credit Shock"
label var crisis_cohort "I(Crisis Cohort)"

egen g = group(group cohort_ig)

eststo: reghdfe lnrevprod c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lnprod c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lnrevdma c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lndma c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lnrevchain c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)
eststo: reghdfe lnchains c.std#c.crisis_cohort if survival1yr==1, absorb(g firm) vce(cl st)

esttab using "Table8A.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear

restore


********************************************************************************
// Table 8 Panel B: Credit-Market Disruption and Outcomes of New Products: Potential Channels
********************************************************************************

preserve
gen n =1 

gcollapse (sum) sumrevenue (mean) parent_code fips_dma_code products_igt survival1yr survival2yr survival3yr std (count) n  if  age_ig>0 & age_ig<17, by(firmgroup group cohort_i cohort_ig crisis_cohort firm st)

tab n
gen lnrev = ln(sumrevenue)
gen lnrevprod = ln(sumrevenue/products_igt)
gen lnprod = ln(products_igt)
gen lnrevchain = ln(sumrevenue/parent_code)
gen lnchains = ln(parent_code)
gen lnrevdma = ln(sumrevenue/fips_dma_code)
gen lndma = ln(fips_dma_code)

label var std "Local Credit Shock"
label var crisis_cohort "I(Crisis Cohort)"

egen g = group(group cohort_ig)

eststo: reghdfe lnrevprod c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)
eststo: reghdfe lnprod c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)
eststo: reghdfe lnrevdma c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)
eststo: reghdfe lndma c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)
eststo: reghdfe lnrevchain c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)
eststo: reghdfe lnchains c.std#c.crisis_cohort if survival1yr==1 & cohort_i!=cohort_ig, absorb(g firm) vce(cl st)

esttab using "Table8B.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear

restore
